package org.ycsoft.metroid;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.ycsoft.MetroidReader.MetroidMetadata;
import org.ycsoft.MetroidReader.MetroidReader;
import org.ycsoft.MetroidReader.StopRecord;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class StopDetails extends Activity{
	

	public TextView city, stopName,nblines,title;
	public ImageView line;
	public ListView lines;
	public String coordinates,mapFile;
	public MetroidReader reader;
	private ProgressDialog dlgProgress;
	public String stop;
	public String cityName;
	public Handler handler;
	public ArrayList<String> stopLinesNames;
	public HashMap<Integer,String> stopLines;
	public ArrayList<Integer> stopLinesIds;


	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.stop);
		city=(TextView)findViewById(R.id.City);
		nblines=(TextView)findViewById(R.id.txtnblines);
		title=(TextView)findViewById(R.id.txtlist);
		lines=(ListView)findViewById(R.id.stoplines);
		stopName=(TextView)findViewById(R.id.stopName);
		line=(ImageView)findViewById(R.id.lineIcon);
		line.setVisibility(View.INVISIBLE);
		title.setVisibility(View.INVISIBLE);
       
		Bundle extras = getIntent().getExtras();
		if (extras != null) 
		{
			stop=extras.getString(Commons.KEY_STOP);
			cityName=extras.getString(Commons.KEY_CITY);
			mapFile=extras.getString(Commons.KEY_MAP);
			stopName.append(stop);
			city.setText(cityName);
			Log.e(Commons.TAG,cityName);
		}
		else
		{
			
			finish();
		}
			
		
		handler = new Handler();
		Thread stopLoader=new Thread(new Runnable()
		{
			public void run() 
			{
				// load map data
				try {
					loadStop();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});
		stopLoader.start();
	}
	
	
	public void loadStop() throws IOException
	{
		synchronized (handler) 
		{
			handler.post(new Runnable() 
			{
				public void run() 
				{
					dlgProgress = ProgressDialog.show(StopDetails.this,Commons.TAG,
							getResources().getString(R.string.load_stop), true, false);
				}
					
			});
			
		}
		reader=new MetroidReader(mapFile);
		reader.loadIndexes(true);
		StopRecord chosenStop=reader.getStopFromId(
				reader.getStopId(stop));
		coordinates=chosenStop.getCoordinates();
		stopLinesIds=chosenStop.getLinesIds();
		stopLinesNames=new ArrayList<String>();
		//Instead of instantiating a set, and then build an arrayList from it, let's build
		//our own set LOL
		String tmp;
		for(int lineId:stopLinesIds)
		{
			tmp=reader.getLineFromId(lineId).getLineName();
			if(!stopLinesNames.contains(tmp))
				stopLinesNames.add(tmp);
		}
		final ArrayAdapter<String> items = new ArrayAdapter<String>(this,
				R.layout.listbold, stopLinesNames);				
		reader.closeStream();
		synchronized (handler) 
		{
			handler.post(new Runnable() 
			{
				public void run() 
				{
					line.setVisibility(View.VISIBLE);
					title.setVisibility(View.VISIBLE);
					nblines.setText(String.valueOf(stopLinesNames.size()));
					lines.setAdapter(items);
					lines.setOnItemClickListener(backToLineDetails);
				}
			});
		}
		dlgProgress.dismiss();
	}
	
	private OnItemClickListener backToLineDetails=new OnItemClickListener()
	{
		public void onItemClick(AdapterView arg0, View arg1, int position,
				long arg3) {
			showLine(position);
			//showAlert("Done?", "ok", "ok", false);
		}
		
	};
	
	public void showLine(int pos)
	{
		Intent i=new Intent(this,LineDetails.class);
		i.putExtra(Commons.KEY_LINE_ID, String.valueOf(stopLinesIds.get(pos)));
		i.putExtra(Commons.KEY_CITY, cityName);
		i.putExtra(Commons.KEY_MAP, mapFile);
		i.putExtra(Commons.KEY_LINE_NAME, (stopLinesNames.get(pos).split(":"))[0].trim());
		startActivity(i);
		finish();
	}
	
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		try{
			if(reader != null && !reader.isStreamClosed())
			{
				reader.closeStream();
			}
		}catch(Exception e){
			Log.e(Commons.TAG,e.getMessage());
		}
	}
	
	public void onpause()
	{
		super.onPause();
		try{
			if(reader != null && !reader.isStreamClosed())
			{
				reader.closeStream();
			}
		}catch(Exception e){
			Log.e(Commons.TAG,e.getMessage());
		}
	}
}
